package top.glike.config;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;

import java.io.File;
import java.io.InputStream;
import java.util.List;

/**
 * SSL证书管理配置类
 * 用于管理阿里云账号、密钥、证书相关配置信息
 */
public class SSLConfig {

    /**
     * 阿里云AccessKey ID
     */
    private String accessKeyId;

    /**
     * 阿里云AccessKey Secret
     */
    private String accessKeySecret;

    /**
     * 阿里云地域ID，默认cn-hangzhou
     */
    private String regionId = "cn-hangzhou";

    /**
     * 证书到期提前检查天数，默认30天
     */
    private int expireCheckDays = 30;

    /**
     * 证书下载保存目录
     */
    private String downloadPath = "./ssl-certs";

    /**
     * 需要监控的域名列表
     */
    private List<String> domains;

    /**
     * 证书申请时的联系邮箱
     */
    private String contactEmail;

    /**
     * 证书申请时的联系电话
     */
    private String contactPhone;

    /**
     * 证书申请时的组织名称
     */
    private String organizationName;

    /**
     * 从配置文件加载配置
     */
    public static SSLConfig loadFromFile(String configFilePath) {
        try {
            if (!FileUtil.exist(configFilePath)) {
                throw new RuntimeException("配置文件不存在: " + configFilePath);
            }

            String configContent = FileUtil.readUtf8String(configFilePath);
            if (StrUtil.isBlank(configContent)) {
                throw new RuntimeException("配置文件内容为空");
            }
            SSLConfig config = JSON.parseObject(configContent, SSLConfig.class);
            config.validate();
            return config;
        } catch (Exception e) {
            throw new RuntimeException("加载配置文件失败: " + e.getMessage(), e);
        }
    }

    /**
     * 验证配置参数
     */
    private void validate() {
        if (StrUtil.isBlank(accessKeyId)) {
            throw new RuntimeException("AccessKey ID不能为空");
        }
        if (StrUtil.isBlank(accessKeySecret)) {
            throw new RuntimeException("AccessKey Secret不能为空");
        }
        if (StrUtil.isBlank(contactEmail)) {
            throw new RuntimeException("联系邮箱不能为空");
        }
        if (domains == null || domains.isEmpty()) {
            throw new RuntimeException("监控域名列表不能为空");
        }

        // 确保下载目录存在
        File downloadDir = new File(downloadPath);
        if (!downloadDir.exists()) {
            downloadDir.mkdirs();
        }
    }

    /**
     * 生成默认配置文件
     */
    public static void generateDefaultConfig(String configFilePath) {
        SSLConfig defaultConfig = new SSLConfig();
        defaultConfig.setAccessKeyId("your-access-key-id");
        defaultConfig.setAccessKeySecret("your-access-key-secret");
        defaultConfig.setContactEmail("your-email@example.com");
        defaultConfig.setContactPhone("13800138000");
        defaultConfig.setOrganizationName("Your Organization");
        defaultConfig.setDomains(java.util.Arrays.asList("example.com", "www.example.com"));

        String configJson = JSON.toJSONString(defaultConfig, true);
        FileUtil.writeUtf8String(configJson, configFilePath);
    }

    // Getter and Setter methods
    public String getAccessKeyId() {
        return accessKeyId;
    }

    public void setAccessKeyId(String accessKeyId) {
        this.accessKeyId = accessKeyId;
    }

    public String getAccessKeySecret() {
        return accessKeySecret;
    }

    public void setAccessKeySecret(String accessKeySecret) {
        this.accessKeySecret = accessKeySecret;
    }

    public String getRegionId() {
        return regionId;
    }

    public void setRegionId(String regionId) {
        this.regionId = regionId;
    }

    public int getExpireCheckDays() {
        return expireCheckDays;
    }

    public void setExpireCheckDays(int expireCheckDays) {
        this.expireCheckDays = expireCheckDays;
    }

    public String getDownloadPath() {
        return downloadPath;
    }

    public void setDownloadPath(String downloadPath) {
        this.downloadPath = downloadPath;
    }

    public List<String> getDomains() {
        return domains;
    }

    public void setDomains(List<String> domains) {
        this.domains = domains;
    }

    public String getContactEmail() {
        return contactEmail;
    }

    public void setContactEmail(String contactEmail) {
        this.contactEmail = contactEmail;
    }

    public String getContactPhone() {
        return contactPhone;
    }

    public void setContactPhone(String contactPhone) {
        this.contactPhone = contactPhone;
    }

    public String getOrganizationName() {
        return organizationName;
    }

    public void setOrganizationName(String organizationName) {
        this.organizationName = organizationName;
    }

    @Override
    public String toString() {
        return "SSLConfig{" +
                "accessKeyId='" + accessKeyId + '\'' +
                ", accessKeySecret='" + accessKeySecret + '\'' +
                ", regionId='" + regionId + '\'' +
                ", expireCheckDays=" + expireCheckDays +
                ", downloadPath='" + downloadPath + '\'' +
                ", domains=" + domains +
                ", contactEmail='" + contactEmail + '\'' +
                ", contactPhone='" + contactPhone + '\'' +
                ", organizationName='" + organizationName + '\'' +
                '}';
    }
}